﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Albon.Model;
using Commonlib;

namespace DAL
{
    public class MenuDAL
    {
        //Menu


        /// <summary>
        /// 添加或修改menu,当ID为0时添加，ID不为0时修改相应menu信息,成功则返回true 
        /// </summary>
        public bool alterMenu(Menu menu)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@menuName", SqlDbType.VarChar,50),
					new SqlParameter("@url", SqlDbType.VarChar,50),
					new SqlParameter("@parentID", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.Text),
                    new SqlParameter("@sequence",SqlDbType.Int),
					new SqlParameter("@active", SqlDbType.Bit)
                                        };
            parameters[0].Value = menu.ID;
            parameters[1].Value = menu.menuName;
            parameters[2].Value = menu.url;
            parameters[3].Value = menu.parentID;
            parameters[4].Value = menu.remark;
            parameters[5].Value = menu.Sequence;
            parameters[6].Value = 1;
            int ret=SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_menualter", parameters);
            return ret > 1;
        }
        /// <summary>
        /// 判断一个菜单名称是否已经存在,id=0是添加时的判断 ，id不等于0是修改时的判断
        /// </summary>
        public bool Exist(int id, string menuname)
        {
            string cmdstr = "";
            if (id != 0)
            {
                cmdstr = "select * from Menu where menuName='"+menuname+"' and id !="+id;
            }
            else
            {
                cmdstr = "select * from Menu where menuName='" + menuname + "'";
            }
            return SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null) != null;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int delMenu(int id)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@id",id),
                new SqlParameter("@ret",SqlDbType.Int)
            };
            parameters[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString,CommandType.StoredProcedure,"p_menudel",parameters);
            return (int)parameters[1].Value;
        }

        //获取一条menu信息
        public DataTable getMenu(string limit)
        {
            string cmdstr = "select * from Menu where active=1 and "+limit+" order by sequence asc";
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
        }
    }
}
